package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingSubsidyDetailEntity;
import com.ly.heart.domain.KingSubsidyEntity;
import com.ly.heart.domain.dto.KingSubsidyDto;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingSubsidyMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingSubsidyDetailService;
import com.ly.heart.service.IKingSubsidyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 出行住宿补助标准 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingSubsidyServiceImpl extends ServiceImpl<KingSubsidyMapper, KingSubsidyEntity> implements IKingSubsidyService {

    @Resource
    private IActModelService iActModelService;
    @Resource
    private IKingSubsidyDetailService iKingSubsidyDetailService;

    @Override
    public List<KingSubsidyEntity> queryList (KingSubsidyEntity kingSubsidyEntity) {
        return null;
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult draft (KingSubsidyDto kingSubsidyDto) {
        KingSubsidyEntity kingSubsidyEntity = kingSubsidyDto.getKingSubsidyEntity ();
        kingSubsidyEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingSubsidyEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingSubsidyEntity.setCreateTime ( DateUtils.getNowDate () );
        updateSubsidyDetail ( kingSubsidyDto.getKingSubsidyDetailEntities (), kingSubsidyEntity.getId () );
        kingSubsidyEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( kingSubsidyEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存出行住宿补助标准失败！" );
        }
        return AjaxResult.success ( "暂存出行住宿补助标准成功", kingSubsidyEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingSubsidyEntity kingSubsidyEntity) {
        if ( StringUtils.isBlank ( kingSubsidyEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingSubsidyEntity.getId () ).
                setBusinessType ( kingSubsidyEntity.getBusinessType () ).setModelId ( kingSubsidyEntity.getModelId () ).
                setBusinessName ( "出行住宿补助标准".concat ( kingSubsidyEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingSubsidyEntity> ().
                set ( KingSubsidyEntity :: getInstanceId, instanceId ).
                set ( KingSubsidyEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                set ( KingSubsidyEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingSubsidyEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingSubsidyEntity :: getId, kingSubsidyEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交出行住宿补助标准失败！" );
        }
        return AjaxResult.success ( "提交出行住宿补助标准成功" );
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult updateSingle (KingSubsidyDto kingSubsidyDto) {
        KingSubsidyEntity kingSubsidyEntity = kingSubsidyDto.getKingSubsidyEntity ();
        kingSubsidyEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingSubsidyEntity.setUpdateTime ( DateUtils.getNowDate () );
        updateSubsidyDetail ( kingSubsidyDto.getKingSubsidyDetailEntities (), kingSubsidyEntity.getId () );
        boolean b = this.updateById ( kingSubsidyEntity );
        if ( ! b ) {
            throw new CustomException ( "修改出行住宿补助标准失败！" );
        }
        return AjaxResult.success ( "修改出行住宿补助标准成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingSubsidyEntity> ().set ( KingSubsidyEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingSubsidyEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingSubsidyEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingSubsidyEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除出行住宿补助标准失败！" );
        }
        return AjaxResult.success ( "删除出行住宿补助标准成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingSubsidyEntity kingSubsidyEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingSubsidyEntity> ().
                set ( KingSubsidyEntity :: getIsDisabled, kingSubsidyEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingSubsidyEntity :: getDisabledTime, DateUtils.getNowDate () ).
                set ( KingSubsidyEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingSubsidyEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( kingSubsidyEntity.getIsDisabled () == 1 ? "禁用出行住宿补助标准失败！" : "解禁出行住宿补助标准失败！" );
        }
        return AjaxResult.success ( kingSubsidyEntity.getIsDisabled () == 1 ? "禁用出行住宿补助标准成功" : "解禁出行住宿补助标准成功" );
    }

    private void updateSubsidyDetail (List<KingSubsidyDetailEntity> kingSubsidyDetailEntities, String subsidyId) {
        if ( ! CollectionUtils.isEmpty ( kingSubsidyDetailEntities ) ) {
            iKingSubsidyDetailService.remove ( new LambdaUpdateWrapper<KingSubsidyDetailEntity> ().eq ( KingSubsidyDetailEntity :: getSubsidyId, subsidyId ) );
            kingSubsidyDetailEntities.forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setSubsidyId ( subsidyId );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = iKingSubsidyDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "更新出行住宿补助标准失败！" );
                }
            } );
        }
    }
}
